﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

using System.Globalization;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class RC_PlacementByExam : System.Web.UI.UserControl
    {

        private object _dataItem = null;

        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

        public string formatDateThai(string value)
        {

            //08/04/2553

            int pos = value.IndexOf("0:00:00");
            if (pos > 1)
            {
                value = value.Replace("00:00:00", "");
                value = value.Replace("0:00:00", "");
            }


            string tmp = null;
            string[] tmp2 = null;
            tmp2 = value.Split('/');
            tmp = tmp2[1] + "/" + tmp2[0] + "/" + Convert.ToString(int.Parse(tmp2[2]));

            return tmp;
        }
        
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {
                bindMovmentCombo();
                bindPosTypeCombo();
                bindClasscombo();
                bindJobSiteCombo();
                bindEntPosTypeCombo();
                bindEntClasscombo();

                this.ViewState.Add("IsUserControlPostBack", true);

            }

            if (DataItem != null)
            {                
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    bindMovmentCombo();
                    bindPosTypeCombo();
                    bindClasscombo();
                    bindJobSiteCombo();
                    bindEntPosTypeCombo();
                    bindEntClasscombo();

                    btnFindTempEmployee.Visible = false;

                    //lblCmdAddSeq.Text = DataBinder.Eval(DataItem, "cmd_add_seq").ToString();
                   // lblTmpEmpSeq.Text = DataBinder.Eval(DataItem, "tmp_emp_seq").ToString();

                    RC_TransferBLL obj = new RC_TransferBLL();
                    DataTable dt;
                    dt = obj.SelectTmpEmpByID(int.Parse(lblTmpEmpSeq.Text));

                    //// ชื่อ  สกุล  //////
                    txttempEmployee.Text = dt.Rows[0]["full_name"].ToString();
                    lblBirthDate.Text = ((DateTime)dt.Rows[0]["birth_date"]).ToString("dd/MM/yyyy", new CultureInfo("th-TH"));
                    lblCitizen.Text = dt.Rows[0]["citizen"].ToString();
                    movmentCombo.SelectedValue = DataBinder.Eval(DataItem, "move_seq").ToString();

                    //  วุฒิการศึกษา //////////
                    BindComboTmpEdu();
                    ComboTmpEdu.SelectedValue = DataBinder.Eval(DataItem, "tmp_edu_seq").ToString();
                    int edu_qual_seq = int.Parse(ComboTmpEdu.SelectedValue);
                    dt = obj.BindTxtMajor(edu_qual_seq);

                    lblMajor.Text = dt.Rows[0]["edu_major_name"].ToString();
                    lblInstitutes.Text = dt.Rows[0]["edu_ins_name"].ToString();

                    ////////////////////////


                    txttempEmployee.Text = DataBinder.Eval(DataItem, "full_name").ToString();

                    // วุฒิการศึกษา

                    //  ComboTmpEdu.SelectedValue = DataBinder.Eval(DataItem, "tmp_edu_seq").ToString();    



                    //ตำแหน่งที่สอบแข่งขันได้

                    //txtEntPosition.Text = DataBinder.Eval(DataItem, "ent_pos_name").ToString();
                    //entPostTypeCombo.SelectedValue = DataBinder.Eval(DataItem, "ent_pos_type_seq").ToString();
                    //entClassCombo.SelectedValue = DataBinder.Eval(DataItem, "ent_class_seq").ToString();
                    //txtEntNo.Text = DataBinder.Eval(DataItem, "ent_no").ToString();
                    //  ent_date = txtEntDate.GetStringValue();


                    //txtEntJobSiteName.Text = DataBinder.Eval(DataItem, "ent_job_site_name ").ToString();

                    string entDate;

                    if (DataBinder.Eval(DataItem, "ent_date").ToString() != "")
                    {
                        entDate = ((DateTime)DataBinder.Eval(DataItem, "ent_date")).ToString("dd/MM/yyyy", new CultureInfo("th-TH"));

                        //txtEntDate.SetValue(entDate);
                    }


                    //ตำแหน่งและส่วนราชการที่รับโอน

                    string strDate;
                    txtPosition.Text = DataBinder.Eval(DataItem, "pos_name").ToString();
                    //txtPosRef.Text = DataBinder.Eval(DataItem, "pos_ref_no").ToString();

                    posTypeCombo.SelectedValue = DataBinder.Eval(DataItem, "pos_type_seq").ToString();
                    classCombo.SelectedValue = DataBinder.Eval(DataItem, "class_seq").ToString();
                    jobSiteCombo.SelectedValue = DataBinder.Eval(DataItem, "job_site_seq").ToString();
                    txtSalary.Text = DataBinder.Eval(DataItem, "salary").ToString();

                    txtRemarks.Text = DataBinder.Eval(DataItem, "remarks").ToString();
                    strDate = ((DateTime)DataBinder.Eval(DataItem, "start_date")).ToString("dd/MM/yyyy", new CultureInfo("th-TH"));


                    //txtStartDate.SetValue(strDate);
                }


                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }


        }

        private void bindClasscombo()
        {

            DataTable dt;
            MoveCommandBLL obj = new MoveCommandBLL();

            dt = obj.SelectClasscombo();

            classCombo.DataValueField = "class_seq";
            classCombo.DataTextField = "class_name";
            classCombo.DataSource = dt;
            classCombo.DataBind();
        }


        private void bindEntClasscombo()
        {

            DataTable dt;
            MoveCommandBLL obj = new MoveCommandBLL();

            dt = obj.SelectClasscombo();

            //entClassCombo.DataValueField = "class_seq";
            //entClassCombo.DataTextField = "class_name";
            //entClassCombo.DataSource = dt;
            //entClassCombo.DataBind();
        }

        private void bindJobSiteCombo()
        {
            DataTable dt;
            MoveCommandBLL obj = new MoveCommandBLL();

            dt = obj.SelecteJobsiteCombo();
            jobSiteCombo.DataValueField = "job_site_seq";
            jobSiteCombo.DataTextField = "job_site_name";
            jobSiteCombo.DataSource = dt;
            jobSiteCombo.DataBind();
        }

        private void bindMovmentCombo()
        {
            IMovmentBLL movBLL = new MovmentBLL();
            IList<MovmentInfo> infoList = movBLL.ListMovments("1");

            movmentCombo.DataSource = infoList;
            movmentCombo.DataBind();
        }

        private void bindEntPosTypeCombo()
        {
            DataTable dt;
            MoveCommandBLL obj = new MoveCommandBLL();

            dt = obj.SelectePosTypeCombo();


            //entPostTypeCombo.DataValueField = "pos_type_seq";
            //entPostTypeCombo.DataTextField = "pos_type_name";
            //entPostTypeCombo.DataSource = dt;
            //entPostTypeCombo.DataBind();
        }

        private void bindPosTypeCombo()
        {
            DataTable dt;
            MoveCommandBLL obj = new MoveCommandBLL();

            dt = obj.SelectePosTypeCombo();

            posTypeCombo.DataValueField = "pos_type_seq";
            posTypeCombo.DataTextField = "pos_type_name";
            posTypeCombo.DataSource = dt;
            posTypeCombo.DataBind();
        }


        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {

            //string start_date;//ตั้งแต่วันที่
            //int cmd_seq, tmp_emp_seq, emp_seq, pos_seq;//รหัสคำสั่ง,รหัส tmp บุคลากร,รหัสตำแหน่ง
            //string pos_ref_no, pos_name;//เลขที่ตำแหน่ง,ชื่อตำแหน่ง
            //int class_seq, job_site_seq;//รหัสระดับ,รหัสหน่วยงาน
            //string job_site_name;//ชื่อหน่วยงาน

            //int tmp_edu_seq;// seq วุฒิการศึกษาที่บรรจุ
            //string ent_pos_name;//สอบแข่งขันได้ลำดับที่,ตำแหน่งที่สอบแข่งขันได้
            //int ent_pos_type_seq, ent_class_seq;//ประภทตำแหน่งที่สอบแข่งขันได้,ระดับตำแหน่งที่สอบแข่งขันได้
            //string ent_job_site_name, ent_date;//ตามประกาศผลสอบของ,ลงวันที่
            //int ent_no;
            //int old_pos_type_seq, old_class_seq; //ประเภทตำแหน่งเดิม,ระดับตำแหน่งเดิม
            //string old_job_site_name, old_pos_name;//สังกัดเดิม,ตำแหน่งเดิม
            //double old_salary, salary; //เงินเดือนเดิม ,เงินเดือน

            //int move_seq, pos_type_seq;//ประเภทการเคลื่อนไหว,ตำแหน่งประเภท
            //string remarks;//หมายเหตุ
            //int created_by;


            //cmd_seq = int.Parse(Session["cmd_seq"].ToString());
            //move_seq = int.Parse(movmentCombo.SelectedValue);

            //// วุฒิการศึกษา
            //tmp_edu_seq = int.Parse(ComboTmpEdu.SelectedValue);



            //salary = Convert.ToDouble(txtSalary.Text);


            //// ตำแหน่งที่สอบแข่งขันได้


            //ent_pos_name = txtEntPosition.Text;
            //ent_pos_type_seq = int.Parse(entPostTypeCombo.SelectedValue);
            //ent_class_seq = int.Parse(entClassCombo.SelectedValue);
            //ent_no = int.Parse(txtEntNo.Text);
            //ent_date = txtEntDate.GetStringValue();
            //ent_job_site_name = txtEntJobSiteName.Text;

            ////ตำแหน่งและส่วนราชการที่รับโอน 

            //pos_name = txtPosition.Text;
            //pos_ref_no = txtPosRef.Text;
            //class_seq = int.Parse(classCombo.SelectedValue);
            //job_site_seq = int.Parse(jobSiteCombo.SelectedValue);
            //job_site_name = jobSiteCombo.Text;
            //start_date = txtStartDate.GetStringValue();
            //remarks = txtRemarks.Text;
            //pos_type_seq = int.Parse(posTypeCombo.SelectedValue);



            //RC_PlacementByExamBLL obj = new RC_PlacementByExamBLL();

            //obj.insert(cmd_seq, int.Parse(lblTmpEmpSeq.Text), move_seq, tmp_edu_seq, salary, pos_name, pos_ref_no,
            //    class_seq, job_site_seq, job_site_name, start_date, pos_type_seq,
            //    remarks, 1, ent_pos_name, ent_pos_type_seq, ent_class_seq, ent_no, ent_date, ent_job_site_name);



        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            //string start_date;//ตั้งแต่วันที่
            //int cmd_seq, tmp_emp_seq, emp_seq, pos_seq;//รหัสคำสั่ง,รหัส tmp บุคลากร,รหัสตำแหน่ง
            //string pos_ref_no, pos_name;//เลขที่ตำแหน่ง,ชื่อตำแหน่ง
            //int class_seq, job_site_seq;//รหัสระดับ,รหัสหน่วยงาน
            //string job_site_name;//ชื่อหน่วยงาน

            //int tmp_edu_seq;// seq วุฒิการศึกษาที่บรรจุ
            //string ent_pos_name;//สอบแข่งขันได้ลำดับที่,ตำแหน่งที่สอบแข่งขันได้
            //int ent_pos_type_seq, ent_class_seq;//ประภทตำแหน่งที่สอบแข่งขันได้,ระดับตำแหน่งที่สอบแข่งขันได้
            //string ent_job_site_name, ent_date;//ตามประกาศผลสอบของ,ลงวันที่
            //int ent_no;
            //int old_pos_type_seq, old_class_seq; //ประเภทตำแหน่งเดิม,ระดับตำแหน่งเดิม
            //string old_job_site_name, old_pos_name;//สังกัดเดิม,ตำแหน่งเดิม
            //double old_salary, salary; //เงินเดือนเดิม ,เงินเดือน

            //int move_seq, pos_type_seq;//ประเภทการเคลื่อนไหว,ตำแหน่งประเภท
            //string remarks;//หมายเหตุ



            //cmd_seq = int.Parse(Session["cmd_seq"].ToString());
            //move_seq = int.Parse(movmentCombo.SelectedValue);

            //// วุฒิการศึกษา
            //tmp_edu_seq = int.Parse(ComboTmpEdu.SelectedValue);


            //salary = Convert.ToDouble(txtSalary.Text);

            //// ตำแหน่งที่สอบแข่งขันได้


            //ent_pos_name = txtEntPosition.Text;
            //ent_pos_type_seq = int.Parse(entPostTypeCombo.SelectedValue);
            //ent_class_seq = int.Parse(entClassCombo.SelectedValue);
            //ent_no = int.Parse(txtEntNo.Text);
            //ent_date = txtEntDate.GetStringValue();
            //ent_job_site_name = txtEntJobSiteName.Text;


            ////ตำแหน่งและส่วนราชการที่รับโอน 

            //pos_name = txtPosition.Text;
            //pos_ref_no = txtPosRef.Text;
            //class_seq = int.Parse(classCombo.SelectedValue);
            //job_site_seq = int.Parse(jobSiteCombo.SelectedValue);
            //job_site_name = jobSiteCombo.Text;
            //start_date = txtStartDate.GetStringValue();
            //remarks = txtRemarks.Text;
            //pos_type_seq = int.Parse(posTypeCombo.SelectedValue);


            //RC_PlacementByExamBLL obj = new RC_PlacementByExamBLL();
            //obj.Update(int.Parse(lblCmdAddSeq.Text), move_seq, tmp_edu_seq, salary, pos_name, pos_ref_no, class_seq, job_site_seq, job_site_name, start_date,
            //    pos_type_seq, remarks, 1, ent_pos_name, ent_pos_type_seq, ent_class_seq, ent_no, ent_date, ent_job_site_name);




        }

        protected void movmentCombo_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }

        protected void oldPosTypeCombo_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }

        protected void posTypeCombo_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }

        protected void jobSiteCombo_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }

        protected void classCombo_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }

        protected void txtSalary_TextChanged(object sender, EventArgs e)
        {

        }

        protected void txtStartDate_TextChanged(object sender, EventArgs e)
        {

        }

        protected void txtOldJobsiteName_TextChanged(object sender, EventArgs e)
        {

        }

        protected void txtOldPosition_TextChanged(object sender, EventArgs e)
        {

        }

        protected void txtOldSalary_TextChanged(object sender, EventArgs e)
        {

        }

        protected void btnFindTempEmployee_Load(object sender, EventArgs e)
        {
            if (DataItem is Telerik.Web.UI.GridInsertionObject)
            {
                btnFindTempEmployee.Attributes.Add("OnClick", "LinkMDdetail(" + HideTempEmployee.ClientID + "," + "'Forms/DialogRC_TmpEmp.aspx','../frmIframe.html',700,500)");
            }



        }

        protected void oldClassCombo_SelectedIndexChanged1(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }



        protected void btnFindTempEmployee_Click1(object sender, ImageClickEventArgs e)
        {

            txttempEmployee.Text = "";
            lblBirthDate.Text = "";
            lblCitizen.Text = "";


            string st = HideTempEmployee.Value;
            if (st == "")
                return;

            lblTmpEmpSeq.Text = st;

            RC_TransferBLL obj = new RC_TransferBLL();

            int cmd_seq = int.Parse(Session["cmd_seq"].ToString());
            //if (obj.chkDup(cmd_seq, int.Parse(st)) == true)
            //{
            //    lblWarningDup.Visible = true;
            //    lblWarningDup.Text = "มีรายชื่อนี้อยู่ในระบบแล้ว";
            //    return;

            //}
            //else
            //{
            //    lblWarningDup.Visible = false;

            //}

            DataTable dt;
            dt = obj.SelectTmpEmpByID(int.Parse(st));
            txttempEmployee.Text = dt.Rows[0]["full_name"].ToString();
            lblBirthDate.Text = ((DateTime)dt.Rows[0]["birth_date"]).ToString("dd/MM/yyyy", new CultureInfo("th-TH"));
            lblCitizen.Text = dt.Rows[0]["citizen"].ToString();


            BindComboTmpEdu();

        }



        private void BindComboTmpEdu()
        {
            DataTable dt;
            RC_TransferBLL obj = new RC_TransferBLL();

            dt = obj.SelectComboTmpEdu(int.Parse(lblTmpEmpSeq.Text), (int?)null);

            ComboTmpEdu.DataValueField = "tmp_edu_seq";
            ComboTmpEdu.DataTextField = "edu_qual_name";
            ComboTmpEdu.DataSource = dt;
            ComboTmpEdu.DataBind();



        }

        protected void ComboTmpEdu_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {

            int edu_qual_seq;
            edu_qual_seq = int.Parse(ComboTmpEdu.SelectedValue);
            DataTable dt;
            RC_TransferBLL obj = new RC_TransferBLL();

            dt = obj.BindTxtMajor(edu_qual_seq);

            lblMajor.Text = dt.Rows[0]["edu_major_name"].ToString();
            lblInstitutes.Text = dt.Rows[0]["edu_ins_name"].ToString();

        }

        protected void txtOldPosition_TextChanged1(object sender, EventArgs e)
        {

        }


        protected void DetailPanel_Load(object sender, EventArgs e)
        {

        }
        
        protected void btnFindPos_Click(object sender, ImageClickEventArgs e)
        {
            string st = HideOldPos.Value;
            if (st == "")
                return;

            RC_TransferBLL obj = new RC_TransferBLL();
            DataTable dt;

            dt = obj.BindPosition(int.Parse(st));

            if (dt.Rows.Count > 0)
            {
                txtPosition.Text = dt.Rows[0]["pos_name"].ToString();
                //txtPosRef.Text = dt.Rows[0]["pos_ref"].ToString();
                posTypeCombo.SelectedValue = dt.Rows[0]["pos_type_seq"].ToString();
                bindClasscombo();
                jobSiteCombo.SelectedValue = dt.Rows[0]["job_site_seq"].ToString();

            }
        }

        protected void btnFindPos_Load(object sender, EventArgs e)
        {

            //if (DataItem is Telerik.Web.UI.GridInsertionObject)
            //{
            btnFindPos.Attributes.Add("OnClick", "LinkMDdetail(" + HideOldPos.ClientID + "," + "'Forms/DialogRC_Pos.aspx','../frmIframe.html',700,500)");
            //}


        }

        protected void txtEntPosition_TextChanged(object sender, EventArgs e)
        {



        }

        protected void entClassCombo_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {


        }

        protected void txtEntJobSiteName_TextChanged(object sender, EventArgs e)
        {

        }

        protected void entPostTypeCombo_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }

        protected void txtEntNo_TextChanged(object sender, EventArgs e)
        {

        }

        protected void txtEntDate_TextChanged(object sender, EventArgs e)
        {

        }

        protected void btnFindEntPos_Click(object sender, ImageClickEventArgs e)
        {

            string st = HideEntPos.Value;
            if (st == "")
                return;

            RC_TransferExamBLL obj = new RC_TransferExamBLL();
            DataTable dt;

            dt = obj.BindPosition(int.Parse(st));

            if (dt.Rows.Count > 0)
            {

                txtEntPosition.Text = dt.Rows[0]["field_pos_name"].ToString();

                if (dt.Rows[0]["pos_seq"].ToString() != "")
                {
                    //entPostTypeCombo.SelectedValue = dt.Rows[0]["pos_seq"].ToString();
                }

            }

        }

        protected void btnFindEntPos_Load(object sender, EventArgs e)
        {

            btnFindEntPos.Attributes.Add("OnClick", "LinkMDdetail(" + HideEntPos.ClientID + "," + "'Forms/DialogRC_OldPos.aspx','../frmIframe.html',700,500)");
        }

        protected void entPostTypeCombo_TextChanged(object sender, EventArgs e)
        {


        }

    }

}

